package offer;

import java.math.BigInteger;

public class Solution67 {
    public int strToInt(String str) {
        str = str.trim();
        if (str.length() == 0)
            return 0;
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) > '9' || str.charAt(i) < '0') {
                str = str.substring(0, i);
                break;
            }
        }
        char c = str.charAt(0);
        if ((c > '9' || c < '0') && c != '+' && c != '-')
            return 0;
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            try {
                BigInteger a = new BigInteger(str);
                if (a.compareTo(new BigInteger("0")) < 0) {
                    return Integer.MIN_VALUE;
                } else {
                    return Integer.MAX_VALUE;
                }
            } catch (Exception e1) {
                return 0;
            }
        }
    }
}
